package com.faxsun.web.controller.admin;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.faxsun.core.pojos.enums.TagType;
import com.faxsun.core.service.TagService;
import com.faxsun.pojos.view.TagTable;
import com.faxsun.web.controller.admin.url.AdminViewConstants;

import mblog.core.pojos.Tag;
import mblog.core.utils.BeanMapUtils;
import mblog.web.controller.BaseController;
import mtons.modules.pojos.Page;

/**
 * 
 * @author songdragon
 *
 */
@Controller
@RequestMapping("/admin/tags")
public class TagsManageController extends BaseController {

	@Autowired
	TagService tagService;

	/**
	 * 添加新的分类标签视图
	 * 
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String addTagView(
			@RequestParam(value = "tagId", required = false) Long tagId,
			@ModelAttribute("tag") Tag tag, ModelMap model) {
		if (tagId != null && tagId != 0L) {
			tag = tagService.getById(tagId);
		}
		// return "/admin/tag_form";
		model.addAttribute(AdminViewConstants.VIEW,
				AdminViewConstants.VIEW_TAG_ADD);
		model.addAttribute("tagTypeList", TagType.list());
		model.addAttribute("tag", tag);

		return AdminViewConstants.VIEW_ADMIN_LAYOUT;
	}

	/**
	 * 保存分类标签
	 * 
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String addTag(@ModelAttribute("tag") Tag tag,
			BindingResult bindingResult) {
		tagService.saveTag(tag);
		return "redirect:/admin/tags/list";
	}

	/**
	 *  列出所有标签 <br/>
	 * ?type=1 分类/导航<br/>
	 * ?type=0 普通<br/>
	 * ?type=2 产品<br/>
	 * 
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(
			@RequestParam(value = "type", required = false) Integer type,
			ModelMap model) {
		/*
		 * TagType tagType = TagType.parse(type); Page page = wrapPage(pageNo);
		 * page.setMaxResults(max); tagService.listTags(tagType, page);
		 * model.addAttribute("tagList", page.getResults()); // return
		 * "/admin/tag_list";
		 */
		String url = "/admin/tags/data-url";
		if (type != null) {
			url += "?type=" + type;
		}
		model.addAttribute("view", AdminViewConstants.VIEW_TAG_LIST);
		model.addAttribute("url", url);
		return AdminViewConstants.VIEW_ADMIN_LAYOUT;
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/data-url", method = RequestMethod.GET)
	public @ResponseBody TagJSON data(
			@RequestParam(value = "type", required = false) Integer type,
			@RequestParam(value = "offset", required = false) Integer pageNo,
			@RequestParam(value = "limit", required = false) Integer max,
			@RequestParam(value = "search", required = false) String search,
			@RequestParam(value = "sort", required = false) String field,
			ModelMap model) {
		TagType tagType = TagType.parse(type);
		if (pageNo == null) {
			pageNo = 1;
		}
		if (max == null) {
			max = 10;
		}
		pageNo = pageNo / max + 1;
		Page page = wrapPage(pageNo);
		page.setMaxResults(max);

		// TODO:按字段排序
		if (StringUtils.isEmpty(search)) {
			tagService.listTags(tagType, page);
		} else {
			// TODO:搜索结果
		}
		TagJSON ret = new TagJSON();
		ret.total = page.getTotalCount();
		ArrayList<TagTable> table = new ArrayList<TagTable>();
		for (Tag tag : (List<Tag>) page.getResults()) {
			table.add(BeanMapUtils.copy(tag));
		}
		ret.rows = table;
		return ret;
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/tagQuery", method = RequestMethod.GET)
	public @ResponseBody List<Tag> queryTag(
			@RequestParam(value = "q", required = false) String q) {
		Page page = wrapPage(1);
		page.setMaxResults(Integer.MAX_VALUE);
		List<Tag> ret = new ArrayList<Tag>();
		if (StringUtils.isEmpty(q)) {
			tagService.listTags(TagType.INVALID, page);
		} else {
			tagService.queryTags(q, page);
		}

		ret = (List<Tag>) page.getResults();

		Collections.sort(ret, new Comparator<Tag>() {
			@Override
			public int compare(Tag o1, Tag o2) {
				return o1.getName().length() - o2.getName().length();
			}
		});
		
		return ret;
	}

	@ModelAttribute("tag")
	public Tag initTagForm() {
		Tag tag = new Tag();
		return tag;
	}
}

class TagJSON {
	public int total;
	public List<TagTable> rows;
}
